package com.luxuewen.sxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luxuewen.sxa.common.annotation.ApiAuth;
import com.luxuewen.sxa.entity.SxaDepartments;
import com.luxuewen.sxa.entity.SxaRoles;
import com.luxuewen.sxa.service.impl.SxaDepartmentsServiceImpl;
import com.luxuewen.sxa.utils.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wen
 */
@RestController
@Slf4j
@Validated
public class DepartmentController {
    @Autowired
    private SxaDepartmentsServiceImpl sxaDepartmentsService;

    @ApiAuth(alias = "dept:create")
    @PostMapping("dept/create")
    public ApiResponse<SxaDepartments> create (@Validated @RequestBody SxaDepartments sxaDepartments) {
        boolean res = sxaDepartmentsService.save(sxaDepartments);
        if (!res) {
            return ApiResponse.fail("部门创建失败！");
        }

        return ApiResponse.success(sxaDepartments);
    }

    @ApiAuth(alias = "dept:update")
    @PostMapping("dept/update")
    public ApiResponse<SxaDepartments> update (@Validated @RequestBody SxaDepartments sxaDepartments) {
        boolean res = sxaDepartmentsService.updateById(sxaDepartments);
        if (!res) {
            return ApiResponse.fail("部门更新失败！");
        }

        return ApiResponse.success(sxaDepartments);
    }

    @ApiAuth(alias = "dept:delete")
    @PostMapping("dept/delete/{id}")
    public ApiResponse<Boolean> delete (@PathVariable Integer id) {
        boolean res = sxaDepartmentsService.removeById(id);
        if (!res) {
            return ApiResponse.fail("部门删除失败！");
        }

        return ApiResponse.success(res);
    }

    @ApiAuth(alias = "dept:list")
    @GetMapping("dept/list")
    public ApiResponse<List<Map<String, Object>>> list (@RequestParam String search) {
        return ApiResponse.success(sxaDepartmentsService.getList(search));
    }

    @ApiAuth(alias = "dept:options")
    @GetMapping("dept/options")
    public ApiResponse<List<Map<String, Object>>> options () {
        List<Map<String, Object>> res = sxaDepartmentsService.options();

        return ApiResponse.success(res);

    }

}
